Repository navigation
BUG: Fix cloning of objects that require constructor arguments - #4262
Merged
stefan6419846 merged 4 commits intoNov 3, 2026
Merged
Conversation
Ak4shh
force-pushed
the
fix/clone-annotation-dictionaries
branch
from
October 27, 2026 01:15
c0712a9 to
ac5cef6
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4262 +/- ##
==========================================
+ Coverage 98.79% 98.81% +0.01%
==========================================
Files 59 59
Lines 12120 12130 +10
Branches 2308 2309 +1
==========================================
+ Hits 11974 11986 +12
Misses 70 70
+ Partials 76 74 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
stefan6419846
requested changes
Oct 28, 2026
Ak4shh
force-pushed
the
fix/clone-annotation-dictionaries
branch
from
October 29, 2026 01:37
ac5cef6 to
9bc7dd5
Compare
Contributor
Author
|
Removed the try/except around inspect.signature(). A Python-level init never triggers it, so it was dead code. |
Contributor
Author
|
Addressed all three points, and also fixed cloning of linked /Next//Prev//N//V objects (with a test). Could you take another look? |
MartinThoma
added a commit
that referenced
this pull request
Nov 2, 2026
Add tests for PageObject branches without coverage so far: * _content_stream_rename() with an inline image: renaming a color space resource called /G renames "/G cs", but keeps /CS /G in the inline image, where it means DeviceGray, * scale() of a page with a /VP viewport, given as array and as dictionary, * extract_text() passing text to visitor_text that is not terminated by an ET operator. Two strict xfail tests document bugs in merge_page(): * #4335: merging into a page whose /Annots is null silently drops the annotations of the merged page, * #3467: merging a page whose annotations were added with PdfWriter.add_annotation() raises TypeError: clone() creates the copy with self.__class__(), but Highlight, FreeText, ... have required keyword-only arguments. The open PRs #3785 and #4262 fix it. Where the expected behavior comes from ISO 32000-2:2020, the docstring names the section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stefan6419846
requested changes
Nov 2, 2026
stefan6419846
left a comment
Collaborator
There was a problem hiding this comment.
Please check the merge conflicts.
MartinThoma
added a commit
that referenced
this pull request
Nov 2, 2026
Add tests for PageObject branches without coverage so far: * _content_stream_rename() with an inline image: renaming a color space resource called /G renames "/G cs", but keeps /CS /G in the inline image, where it means DeviceGray, * scale() of a page with a /VP viewport, given as array and as dictionary, * extract_text() passing text to visitor_text that is not terminated by an ET operator. Two strict xfail tests document bugs in merge_page(): * #4335: merging into a page whose /Annots is null silently drops the annotations of the merged page, * #3467: merging a page whose annotations were added with PdfWriter.add_annotation() raises TypeError: clone() creates the copy with self.__class__(), but Highlight, FreeText, ... have required keyword-only arguments. The open PRs #3785 and #4262 fix it. Where the expected behavior comes from ISO 32000-2:2020, the docstring names the section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stefan6419846
pushed a commit
that referenced
this pull request
Nov 2, 2026
…#4338) Add tests for PageObject branches without coverage so far: * _content_stream_rename() with an inline image: renaming a color space resource called /G renames "/G cs", but keeps /CS /G in the inline image, where it means DeviceGray, * scale() of a page with a /VP viewport, given as array and as dictionary, * extract_text() passing text to visitor_text that is not terminated by an ET operator. Two strict xfail tests document bugs in merge_page(): * #4335: merging into a page whose /Annots is null silently drops the annotations of the merged page, * #3467: merging a page whose annotations were added with PdfWriter.add_annotation() raises TypeError: clone() creates the copy with self.__class__(), but Highlight, FreeText, ... have required keyword-only arguments. The open PRs #3785 and #4262 fix it. Where the expected behavior comes from ISO 32000-2:2020, the docstring names the section. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
DictionaryObject.clone() and DictionaryObject.replicate() created the target object via self.__class__(). That fails for every annotation class (Text, FreeText, Link, Polygon, ...), because their constructors require arguments, so PageObject.merge_page() raised TypeError whenever the merged page carried a markup annotation. Create the empty target through a helper that skips __init__ when the class cannot be instantiated without arguments. The clone is populated from the source object right afterwards, so the constructor arguments are not needed; the annotation constructors only store their arguments in the dictionary anyway. Fixes py-pdf#3467. Signed-off-by: Ak4shh <akashposa40@gmail.com>
Linked entries such as /Next, /Prev, /N and /V were still instantiated via `__class__()`, failing for classes like OutlineItem. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Ak4shh <akashposa40@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Ak4shh <akashposa40@gmail.com>
Ak4shh
force-pushed
the
fix/clone-annotation-dictionaries
branch
from
November 3, 2026 00:03
7d3deae to
18da6d4
Compare
Signed-off-by: Ak4shh <akashposa40@gmail.com>
Ak4shh
force-pushed
the
fix/clone-annotation-dictionaries
branch
2 times, most recently
from
November 3, 2026 00:10
65dc82f to
bc3d470
Compare
stefan6419846
approved these changes
Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3467.
The bug
PageObject.merge_page()clones the annotations of the page passed to it.DictionaryObject.clone()built the clone target withself.__class__(),which is impossible for every annotation class, because their constructors
require arguments:
The same
self.__class__()call was inDictionaryObject.replicate(), so thisaffected any clone/replicate of an annotation dictionary, not just
merge_page.The fix
DictionaryObject._new_for_clone()creates the empty target. When the classcannot be instantiated without arguments, it instantiates it without calling
__init__; the clone is populated from the source object immediatelyafterwards, so the constructor arguments are not needed. Annotation
constructors only store their arguments in the dictionary anyway
(
self["/Vertices"] = ...), which_clonethen copies from the source.The check is signature-based (and cached per class) rather than
try/except TypeError, becauseLink()raisesValueErrorwhen neitherurlnortarget_page_indexis given.Classes that can be built without arguments (e.g.
PageObject, which setspdfin__init__) keep taking the normal construction path.Tests
tests/test_page.py::test_merge_page_with_annotations_of_merged_page— theissue's reproducer: merge a page that carries a
Polygonannotation, thenassert the annotation survives a write/re-read round trip.
tests/test_annotations.py::test_clone_and_replicate_annotation—parametrized over all ten annotation classes (
Ellipse,FreeText,Highlight,Line,Link,Polygon,PolyLine,Popup,Rectangle,Text), asserting thatclone()andreplicate()both return the sameclass with the same keys and that the copy does not share entries with the
source.
Both tests fail on
mainand pass with this change:Full suite
python -m pytest tests -q --basetemp=...on Windows / Python 3.13:31 failed, 2158 passed, 34 skipped, 95 errors— identical failures and errorsto the unmodified tree (all pre-existing and environmental: empty
sample-files/submodule, missingfontTools,pytest-cache/tmp_pathpermission issues).
passedrises by exactly the 11 new test cases.Scope
Three files:
pypdf/generic/_data_structures.py,tests/test_annotations.py,tests/test_page.py.